package com.anzia.list;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

/**
 *@ClassName List_Demo
 *@Description
 *@Author Anzia.
 *@Date 2021/4/10 10:03
 *@Version 1.0
 **/
public class List_Demo {
    public static void main(String[] args) {
        //创建对象
        List list = new ArrayList();

        //添加元素
        list.add("华为");
        list.add("苹果");
        list.add("小米");

        System.out.println(list.size());
        System.out.println(list.toString());

        //删除
        //list.remove("小米");
        //list.remove(2);
        //list.clear();
        //System.out.println("删除之后：" + list);

        //遍历
        //使用for
        System.out.println("---------");
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
        //使用增强for
        System.out.println("---------");
        for (Object o : list) {
            System.out.println(o);
        }
        //使用迭代器
        System.out.println("---------");
        Iterator Iterator = list.iterator();
        while (Iterator.hasNext()) {
            Object next = Iterator.next();
            System.out.println(next);
        }
        System.out.println("---------");
        //使用列表迭代器，和Iterator的区别，ListIterator可以向前或向后遍历、添加、删除、修改元素
        ListIterator listIterator = list.listIterator();
        while (listIterator.hasNext()) {
            System.out.println(listIterator.nextIndex() + " : " + listIterator.next());
        }
        System.out.println("---------");
        while (listIterator.hasPrevious()) {
            System.out.println(listIterator.previousIndex() + " : " + listIterator.previous());
        }

        //判断
        System.out.println(list.contains("华为"));
        System.out.println(list.isEmpty());

        //获取位置
        System.out.println(list.indexOf("华为"));
    }
}
